package org.unisinos.gc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import org.unisinos.gc.vo.Area;
import org.unisinos.gc.vo.Congresso;
import org.unisinos.gc.vo.SubArea;

public abstract class SubAreaDAO {

	public static Collection<SubArea> getSubAreaList() throws SQLException {
		Collection<SubArea> areaList = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = ConnectionManager.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select sa.*, a.nome as nomeArea from subarea sa, area a where sa.codigoArea = a.codigo");
			areaList = new ArrayList<SubArea>();
			while(rs.next()) {
				areaList.add(new SubArea(new Area(rs.getInt("codigoArea"), rs.getString("nomeArea")), rs.getInt("codigo"), rs.getString("nome")));
			}
		}			
		finally {
			try {
				if (con!=null)con.close();
				if (stmt!=null)stmt.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		return areaList;
	}
	
	public static Collection<SubArea> getSubAreaListOrdered(int congressoID) throws SQLException {
		Collection<SubArea> areaList = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = ConnectionManager.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select sa.*, " +
										  "a.nome as nomeArea " +
									 "from subarea sa, " +
									 	  "area a, " +
									 	  "congresso_area ca " +
									"where sa.codigoArea = a.codigo " +
									"and ca.codigo_area = sa.codigoarea " +
									"and ca.codigo_congresso = " + congressoID +
									" order by a.nome, sa.nome");
			areaList = new ArrayList<SubArea>();
			while(rs.next()) {
				areaList.add(new SubArea(new Area(rs.getInt("codigoArea"), rs.getString("nomeArea")), rs.getInt("codigo"), rs.getString("nome")));
			}
		}			
		finally {
			try {
				if (con!=null)con.close();
				if (stmt!=null)stmt.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		return areaList;
	}
	
	public static SubArea getSubArea(int codigo) throws SQLException {
		SubArea subarea = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("select * from subarea where codigo = ?");
			ps.setInt(1, codigo);
			rs = ps.executeQuery();
			while(rs.next()) {
				subarea = new SubArea(new Area(rs.getInt("codigoArea"), null), rs.getInt("codigo"), rs.getString("nome"));
				break;
			}
			
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		return subarea;
	}
	
	public static void addSubArea(SubArea subarea) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("INSERT INTO subarea(codigo, nome, codigoArea) VALUES (?, ?, ?)");
			ps.setInt(1, subarea.getCodigo());
			ps.setString(2, subarea.getNome());
			ps.setInt(3, subarea.getArea().getCodigo());
			ps.execute();
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
			}catch (Exception e) {}
		}
	}
	
	public static void deleteSubArea(int codigo) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("delete from subarea where codigo = ?");
			ps.setInt(1, codigo);
			ps.execute();
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
			}catch (Exception e) {}
		}
	}
	
	public static void updateSubArea(SubArea area) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("UPDATE subarea SET nome=?, codigoArea = ? WHERE codigo=?");
			ps.setString(1, area.getNome());
			ps.setInt(2, area.getArea().getCodigo());
			ps.setInt(3, area.getCodigo());
			ps.execute();
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
			}catch (Exception e) {}
		}
	}
}
